package android.content;

import android.content.impl.SharedPreferencesImpl;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import io.reactivex.annotations.NonNull;

/**
 * @author slc
 * @date 2022/03/30 下午 3:35
 */
public class ContentResolver {
    private final SharedPreferencesImpl sharedPreferences = new SharedPreferencesImpl();

    public ContentResolver() {

    }

    public final int delete(Uri uri, String key, String[] param) {
        sharedPreferences.edit().remove(formatKey(uri, key)).apply();
        return 0;
    }

    public final @NonNull Uri insert(Uri uri, ContentValues contentValues) {
        sharedPreferences.edit().put(formatKey(uri, (String) contentValues.get("key")), contentValues.get("value")).apply();
        return uri;
    }

    public final @NonNull
    Cursor query(Uri uri,
                 String[] projection, String selection,
                 String[] selectionArgs, String sortOrder) {
        String key = projection[0];
        Cursor cursor = new MatrixCursor();
        cursor.setKey(key);
        cursor.setValue((String) sharedPreferences.getString(formatKey(uri, key), null));
        return cursor;
    }


    private static String formatKey(Uri uri, String key) {
        return uri.getUriStr() + ":" + key;
    }
}
